package com.ctg.sysmgr.sso.server.common.uitl;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SerializeUtil {
	
	
	private final static Logger logger = LoggerFactory.getLogger(SerializeUtil.class);
    /**
     * 序列化
     * 
     * @param object
     * @return
     */
    public static byte[] serialize(Object object) {
    	if(object == null){
    		return null;
    	}
    	
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
        	logger.error("序列化ticket的时候发生了错误:"+e.getMessage() + "object:"+object.toString());
        }finally{
        	if(baos != null){
        		try {
					baos.close();
				} catch (IOException e) {
				}
        	}
        	if(oos != null){
        		try {
					oos.close();
				} catch (IOException e) {
				}
        	}
        	
        }
        return null;
    }

    /**
     * 反序列化
     * 
     * @param bytes
     * @return
     */
    public static Object unserialize(byte[] bytes) {
    	if(bytes == null || bytes.length < 0){
    		return null;
    	}
    	
    	
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
        	logger.error("反序列化的时候发生了错误:"+e.getMessage() + "bytes:"+bytes.toString());
        }finally{
        	if(bais != null){
        		try {
					bais.close();
				} catch (IOException e) {
				}
        	}
        	if(ois != null){
        		try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
        }
        return null;
    }
    
    
    /**
     * 序列化，输出的是string
     * @param o
     * @return
     */
    public static String serializeString(Object object){
    	if(object == null){
    		return null;
    	}
    	 ObjectOutputStream oos = null;
         ByteArrayOutputStream baos = null;
         try {
             // 序列化
             baos = new ByteArrayOutputStream();
             oos = new ObjectOutputStream(baos);
             oos.writeObject(object);
             String str = baos.toString("ISO-8859-1");
             str = java.net.URLEncoder.encode(str, "UTF-8");
             return str;
         } catch (Exception e) {
         	logger.error("序列化ticket的时候发生了错误:"+e.getMessage() + "object:"+object.toString());
         }
         return null;
    	
    }
    
    /**
     * 反序列化
     */
    public static Object unserialize(String serStr){
    	if(serStr == null || "".equals("")){
    		return null;
    	}
    	String redStr = null;
    	ByteArrayInputStream byteArrayInputStream = null;
    	ObjectInputStream objectInputStream = null;
    	Object obj = null;
    	try{
    		redStr = java.net.URLDecoder.decode(serStr, "UTF-8");
            byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
            objectInputStream = new ObjectInputStream(byteArrayInputStream); 
            obj = objectInputStream.readObject();
    	}catch(Exception e){
    		
    	}finally{
    		if(objectInputStream != null){
    			try {
					objectInputStream.close();
				} catch (IOException e) {
				}
    		}
    		if(byteArrayInputStream != null){
    			try {
					byteArrayInputStream.close();
				} catch (IOException e) {
				}
    		}
            
    	}
        return obj;
    }
    
    
    
}